﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace FlyWWPF
{
    public class FlyWindow: Window
    {
        private Button CloseButton;//关闭按钮
        private Button MinButton;//最小化按钮
        private Button MaxButton;//最大化按钮
        private TextBlock WindowTitleTbl;//标题

        public FlyWindow()
        {
            this.Loaded += FlyWindow_Loaded;
        }

        private void FlyWindow_Loaded(object sender, RoutedEventArgs e)
        {
            // 查找窗体模板
            ControlTemplate metroWindowTemplate
                    = App.Current.Resources["MetroWindowTemplate"] as ControlTemplate;

            if (metroWindowTemplate != null)
            {
                CloseButton = metroWindowTemplate.FindName("CloseWinButton", this) as Button;
                MinButton = metroWindowTemplate.FindName("MinWinButton", this) as Button;
                MaxButton= metroWindowTemplate.FindName("MaxWinButton", this) as Button;
                CloseButton.Click += CloseButton_Click;
                MinButton.Click += MinButton_Click;
                MaxButton.Click += MaxButton_Click;

                WindowTitleTbl = metroWindowTemplate.FindName("WindowTitleTbl", this) as TextBlock;
            }
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        }

        //关闭事件
        private void CloseButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Close();
        }

        //最小化事件
        private void MinButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.WindowState = System.Windows.WindowState.Minimized;
        }

        //最大化/正常化事件
        private void MaxButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (this.WindowState == System.Windows.WindowState.Maximized)
            {
                this.WindowState = System.Windows.WindowState.Normal;
            }
            else
            {
                this.WindowState = System.Windows.WindowState.Maximized;
            }
            
        }

        /// <summary>
        /// 实现窗体移动
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            DragMove();
            base.OnMouseLeftButtonDown(e);
        }
    }
}
